package com.example.controller;


import com.example.domain.Prescription;
import com.example.domain.Prescription2;
import com.example.service.PrescriptionService;
import com.example.service.PrescriptionService2;
import com.example.utils.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.example.utils.AjaxResult.*;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/root")
public class PrescriptionController {

    @Autowired
    private PrescriptionService prescriptionService;

    @Autowired
    private PrescriptionService2 prescriptionService2;

    /** 药品管理 */
    @RequestMapping("/prescription")
    public AjaxResult prescription(@RequestParam(defaultValue = "1", value = "page") Integer pageNum,
                                   @RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
                                   @RequestParam(defaultValue = "" , value = "prescription") String usr, HttpServletRequest request
    ){
        int patient_id =(int) request.getSession().getAttribute("patient_id");
        System.out.println("asdiuhaskudh"+patient_id);
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        if (usr == null || usr.equals("")) {
            map.put("usr","");
            List<Prescription> prescription = prescriptionService.listAll(patient_id);
            System.out.println(prescription.toString());
            PageInfo<Prescription> page = new PageInfo<>(prescription);
            return successData(page.getTotal(), prescription);
        }
        map.put("usr",usr);
        List<Prescription> prescription = prescriptionService.listAll(patient_id);
        PageInfo<Prescription> page = new PageInfo<>(prescription);
        return successData(page.getTotal(), prescription);
    }


    /** 药品管理 */
    @RequestMapping("/prescription2")
    public AjaxResult prescription2(@RequestParam(defaultValue = "1", value = "page") Integer pageNum,
                                   @RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
                                   @RequestParam(defaultValue = "" , value = "prescription2") String usr
    ){
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        if (usr == null || usr.equals("")) {
            map.put("usr","");
            List<Prescription2> prescription2 = prescriptionService2.listAll(1);
            System.out.println(prescription2.toString());
            PageInfo<Prescription2> page = new PageInfo<>(prescription2);
            return successData(page.getTotal(), prescription2);
        }
        map.put("usr",usr);
        List<Prescription2> prescription2 = prescriptionService2.listAll(1);
        PageInfo<Prescription2> page = new PageInfo<>(prescription2);
        return successData(page.getTotal(), prescription2);
    }

    /** 新增添加 */
    @RequestMapping("/addPrescription")
    public AjaxResult addPrescription(Prescription2 prescription){
        System.out.println("test1");
//        System.out.println(prescription.getIll_case());
        int insert = prescriptionService2.insert(prescription);
        if (1 == insert){
            return success("添加成功");
        }
        return error("添加失败");
    }

    /** 处方修改 */
    @RequestMapping("/modifyPrescription")
    public AjaxResult modifyPrescription(Prescription2 prescription){

        int update = prescriptionService2.update(prescription);

        if (1 == update){
            return success("修改成功");
        }
        return error("修改失败");
    }
}
